package com.sc.byahi.user.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;

public class ResizedUpload extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger("com.sc.byahi.user.server.ResizedUpload");

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		List<BlobKey> newImages = StoreUtil.getUploadedBlobs(req);
		logger.log(Level.INFO, "Uploaded resized images received: ", newImages);
		res.setStatus(HttpServletResponse.SC_OK);
		res.setContentType("application/octet-stream");
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(byteOut);
		oos.writeObject(newImages);
		oos.flush();
		oos.close();
		byte[] buf = byteOut.toByteArray();
		res.setContentLength(buf.length);
		ServletOutputStream servletOut = res.getOutputStream();
		servletOut.write(buf);
		servletOut.flush();
	}
}
